#include <LNM8F01.h>
#include "beep_control.h"
#include "system_init.h"

// 蜂鸣器状态和定时变量
static BeepState beepState = BEEP_IDLE;
static uint32_t beepStartTime = 0;

void init_pwm4(void)
{

    PBHSEL &= ~(0x03);
    PBHSEL |= 0x01;
    PWMOEN |= 0x01 << 4;    // 使能PWM4
	
    PWMCK &= ~(0x01 << 5);  // 选择系统时钟
    PWMCK |= 0x01 << 3;     // 使能PWM时钟

    PWMCON1 |= 0x01 << 3;   // [3] PWM自动重载
    PWMCON1 &= ~(0x01 << 6);// [7:6]普通模式
                            // [5] 一次模式
    PWMCON1 &= ~(0x01 << 4);// [4] 关闭中断

#ifdef PPL_24M	
		//24M/6K = 4000
		PWMPH |= 0x17;					// 设定周期高字节
    PWMPL |= 0x70;        
    PWM4DUH |= 0x0b;        // 占空比高字节
    PWM4DUL |= 0xb8;        // 占空比低字节	
#else
		//8M/2K = 4000
		PWMPH |= 0x12;					// 设定周期高字节
    PWMPL |= 0xBF;        
    PWM4DUH |= 0x0E;        // 占空比高字节
    PWM4DUL |= 0xFE;        // 占空比低字节
#endif
	    
    PWMCON0 |= 0x01;        // 加载周期和占空比
    while (PWMCON0 & 0x01); // 等待加载完成
}

void enable_pwm4(void)
{
    PWMCON0 |= 0x01 << 7;   // 使能TIM4 PWM
}

void disable_pwm4(void)
{
    PWMCON0 &= ~(0x01 << 7);  // 关闭TIM4 PWM
}

// 蜂鸣器初始化
void init_beep(void)
{
    init_pwm4();
}

// 蜂鸣器短鸣一声
void play_beep_short(void)
{
    if (beepState == BEEP_IDLE) {
        beepState = BEEP_SHORT_ON;
        beepStartTime = get_system_time();
				enable_pwm4();  // 打开蜂鸣器
    }
}

// 蜂鸣器急促双鸣
void play_beep_double(void)
{
    if (beepState == BEEP_IDLE) {
        beepState = BEEP_DOUBLE_ON1;
        beepStartTime = get_system_time();
				enable_pwm4();  // 打开蜂鸣器
    }
}

// 蜂鸣器长鸣
void play_beep_long(void)
{
    if (beepState == BEEP_IDLE) {
        beepState = BEEP_LONG_ON;
        beepStartTime = get_system_time();
				enable_pwm4();  // 打开蜂鸣器
    }
}

// 蜂鸣器状态机更新
void update_beep_state(void)
{
    uint32_t currentTime = get_system_time();
    
    switch (beepState) {
        case BEEP_IDLE:
            break;
            
        case BEEP_SHORT_ON:
            if (currentTime - beepStartTime >= 150) {  // 150ms
                beepState = BEEP_SHORT_OFF;
                beepStartTime = currentTime;
								disable_pwm4();   // 关闭蜂鸣器
            }
            break;
            
        case BEEP_SHORT_OFF:
//            if (currentTime - beepStartTime >= 150) {   // 150ms
                beepState = BEEP_IDLE;  
//            }
            break;
            
        case BEEP_DOUBLE_ON1:
            if (currentTime - beepStartTime >= 50) {   // 50ms
                beepState = BEEP_DOUBLE_OFF1;
                beepStartTime = currentTime;
								disable_pwm4();   // 关闭蜂鸣器
            }
            break;
            
        case BEEP_DOUBLE_OFF1:
            if (currentTime - beepStartTime >= 25) {   // 25ms
                beepState = BEEP_DOUBLE_ON2;
                beepStartTime = currentTime;
								enable_pwm4();  // 打开蜂鸣器
            }
            break;
            
        case BEEP_DOUBLE_ON2:
            if (currentTime - beepStartTime >= 50) {   // 50ms
                beepState = BEEP_DOUBLE_OFF2;
                beepStartTime = currentTime;
								disable_pwm4();   // 关闭蜂鸣器
            }
            break;
            
        case BEEP_DOUBLE_OFF2:     
//            if (currentTime - beepStartTime >= 25) {   // 25ms
                beepState = BEEP_IDLE;
//            }
            break;
						
				case BEEP_LONG_ON:
            if (currentTime - beepStartTime >= 300) {   // 300ms
                beepState = BEEP_DOUBLE_OFF2;
                beepStartTime = currentTime;
								disable_pwm4();   // 关闭蜂鸣器
            }
					break;
				
				case BEEP_LONG_OFF:
 //           if (currentTime - beepStartTime >= 100) {   // 100ms
                beepState = BEEP_IDLE;
 //           }
					break;
				
				default:
					break;
    }
}
